using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace myGame
{
    public class Principale : Moveable
    {
        private float posX, posY;
        int directiony = 0, directionx = 0;

        SpriteBatch spriteBatch;
        KeyboardState myKeyboardState;
        KeyboardState oldKeyboardState;

        public Principale(Game game)
            : base(game)
        {
        }
        public Principale(Game game, int _x, int _y, int _v)
            : base(game, _x, _y, _v)
        {
        }

        public override void Initialize()
        {
            posX = 0;
            posY = 0;
            base.Initialize();
        }

        public void attaque(List<Pokemom> pk)
        {
            if (pk.Count == 0)
            {
                vitesse = 0;
                return;
            }
            double plus_courte = 10000000;
            int index = 0;
            for (int i = 0; i < pk.Count; i++)
            {
                if (plus_courte > getDistanceTo(pk[i]))
                {
                    plus_courte = getDistanceTo(pk[i]);
                    index = i;
                }
            }
            if (posX < pk[index].getX())
                posX += vitesse+1;
            if (posX > pk[index].getX())
                posX -= vitesse+1;
            if (posY < pk[index].getY())
                posY += vitesse+1;
            if (posY > pk[index].getY())
                posY -= vitesse+1;
        }

        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);
            texture = Game.Content.Load<Texture2D>("image/pikachu1");
            posY = GraphicsDevice.Viewport.Height / 2;
            base.LoadContent();
        }

        public bool eat(Pokemom pk)
        {
            if (pk.getTexture().Width*pk.getScale() > this.texture.Width)
                return false;
            double distance = getDistanceTo(pk);
            if (distance < texture.Height / 2 + pk.getTexture().Height * pk.getScale())
            {
                return true;
            }
            return false;
        }

        public double getDistanceTo(Pokemom pk)
        {
            return Math.Sqrt((posX - pk.getX()) * (posX - pk.getX()) + (posY - pk.getY()) * (posY - pk.getY()));
        }

        public double distance(int x1, int y1, int x2, int y2)
        {
            return Math.Sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
        }

        public override void Update(GameTime gameTime)
        {
            myKeyboardState = Keyboard.GetState();

            if (myKeyboardState.IsKeyDown(Keys.F4))
                Game.Exit();

            if (posX > 0 && posX < GraphicsDevice.Viewport.Width - texture.Width && vitesse != 0)
            {
                posX = posX + directionx;
            }
            else { 
                directionx = -directionx;
                posX = posX + vitesse*directionx;
            }
            if (posY > 0 && posY < GraphicsDevice.Viewport.Height - texture.Height)
            {
                posY = posY + directiony;
            }
            else
            {
                directiony = -directiony;
                posY = posY + vitesse*directiony;
            }

            if (myKeyboardState.IsKeyDown(Keys.Up))
            {
                directiony = -vitesse;
                directionx = 0;
                posY--;
            }
            else if (oldKeyboardState.IsKeyDown(Keys.Up))
            {

            }

            if (myKeyboardState.IsKeyDown(Keys.Down))
            {
                directiony = vitesse;
                directionx = 0;
                posY++;
            }
            else if (oldKeyboardState.IsKeyDown(Keys.Down))
            {
            }

            if (myKeyboardState.IsKeyDown(Keys.Left))
            {
                directionx = -vitesse;
                directiony = 0;
                posX--;
            }
            else if (oldKeyboardState.IsKeyDown(Keys.Left))
            {
            }
            if (myKeyboardState.IsKeyDown(Keys.Right))
            {
                directionx = vitesse;
                directiony = 0;
                posX++;
            }
            else if (oldKeyboardState.IsKeyDown(Keys.Right))
            {
            }



            oldKeyboardState = myKeyboardState;


            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            spriteBatch.Begin();
            spriteBatch.Draw(texture, new Vector2(posX, posY), Color.White);
            spriteBatch.End();
            base.Draw(gameTime);
        }
    }
}
